package com.qzsoft.zhjd.warning.modules.warningcementconfig.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.warning.modules.warningcementconfig.dto.WarningCementConfigListDto;
import com.qzsoft.zhjd.warning.modules.warningcementconfig.entity.WarningCementConfigEntity;
import com.qzsoft.zhjd.warning.modules.warningcementconfig.entity.WarningCementUserEntity;
import com.qzsoft.zhjd.warning.modules.warningcementconfig.mapper.WarningCementConfigMapper;
import com.qzsoft.zhjd.warning.modules.warningcementconfig.service.WarningCementConfigService;
import com.qzsoft.zhjd.warning.modules.warningcementconfig.service.WarningCementUserService;

/**
 * @ClassName WarningCementConfigServiceImpl
 * @Description 水稳预警业务接口实现
 * @author linhuawei
 * @since 2022-05-16
 */
@Service
public class WarningCementConfigServiceImpl extends ServiceImpl<WarningCementConfigMapper, WarningCementConfigEntity>
        implements WarningCementConfigService {

    @Autowired
    private WarningCementUserService warningCementUserService;
    
    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    @ZhjdDataFilter(tableAlias = "t2")
    public IPage<WarningCementConfigListDto> pageDto(Map<String, Object> params) {
        IPage<WarningCementConfigListDto> page = baseServiceComponent.getPage(params, WarningCementConfigListDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    public WarningCementConfigEntity getInfo(String id) {
        WarningCementConfigEntity entity = baseMapper.selectById(id);
        if (null != entity) {
            List<WarningCementUserEntity> users = warningCementUserService.listByConfigId(id);
            entity.setUsers(users);
        }
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(WarningCementConfigEntity entity) {
        // 提取用户推送信息
        coverInfo(entity);

        // 保存预警数据
        baseMapper.insert(entity);

        // 保存用户数据
        if (null != entity.getUsers()) {
            String configId = entity.getId();
            List<WarningCementUserEntity> users = entity.getUsers();
            for (WarningCementUserEntity user : users) {
                user.setId(null);
                user.setConfigId(configId);
            }
            warningCementUserService.saveBatch(users);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(WarningCementConfigEntity entity) {
        // 提取用户推送信息
        coverInfo(entity);

        // 删除用户信息
        warningCementUserService.deleteByConfigId(entity.getId());

        // 更新配置信息
        baseMapper.updateById(entity);

        // 添加用户信息
        if (null != entity.getUsers()) {
            String configId = entity.getId();
            List<WarningCementUserEntity> users = entity.getUsers();
            for (WarningCementUserEntity user : users) {
                user.setId(null);
                user.setConfigId(configId);
            }
            warningCementUserService.saveBatch(users);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        // 判断要删除的数据是否存在
        if (null == id) {
            return true;
        }

        // 删除用户信息
        warningCementUserService.deleteByConfigId(id);

        // 删除配置信息
        baseMapper.deleteById(id);
        return true;
    }

    /**
     * @Title coverInfo
     * @Description 提取推送用户信息
     * @param entity
     */
    private void coverInfo(WarningCementConfigEntity entity) {
        entity.setInfo(null);
        if (null != entity.getUsers()) {
            StringBuffer sbf = new StringBuffer();
            for (WarningCementUserEntity user : entity.getUsers()) {
                sbf.append(user.getName());
                sbf.append(";");
            }
            entity.setInfo(sbf.toString());
        }
    }

}
